package com.ebt.m.customer.ui;

import com.ebt.m.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.TextView;

public class DialogWaitProgress extends Dialog{

	private static final int DEFAULT_SHOW_DURATION = 1000;
	private static final int DISMISS_DIALOG = 0;
	
	private String textMessage;
	private long showAtTime;
	private Handler handler;
	
	private void initHandler(){

		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {

				switch(msg.what){
				case DISMISS_DIALOG:
					DialogWaitProgress.this.dismiss();
					break;
				}
			}
		};
	}
	
	public DialogWaitProgress(Context context) {
		super(context, R.style.loading_dialog);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	public void setTextMessage(String textMessage){
		this.textMessage = textMessage;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_customer_wait_progress);
		if(this.textMessage != null){
			((TextView)findViewById(R.id.text)).setText(textMessage);
		}
		showAtTime = System.currentTimeMillis();
	}
	
	public long getShowDuration(){
		return System.currentTimeMillis() - showAtTime;
	}
	
	public void dismissWaitDialog() {
		if(getShowDuration() >= DEFAULT_SHOW_DURATION){
			this.dismiss();
		}else{
			initHandler();
			handler.sendEmptyMessageDelayed(DISMISS_DIALOG, DEFAULT_SHOW_DURATION - getShowDuration());
		}
	}
	
}
